//index.js
//获取应用实例
var app = getApp();
Page({
  data: {
    window: {},
    page: 1,
    listData: [],
    listData2:[],
    tabId:0,
    type: 1,
    notiCount:0,
    laspage: 0,
    loading: 0,
    loaded:0,
    loaded2:0,
    laspage2: 0,
    loading2: 0,
    page2:1,
    user_id:0
  },

  //加载更多
  scrolltolower: function (e) {
    console.log(e);
    this.getList();
  },
  indexTab: function (e) {
    var id = e.currentTarget.dataset.id;
    this.setData({ tabId: id });
    if ((id == 0 && this.data.loaded == 0) || (id == 1 && this.data.loaded2 == 0)) {
      this.getList();
    }
    wx.setNavigationBarTitle({
      title: id == 0 ? '我赞过的' : '赞过我的'
    });
  },



  swiperChange: function (e) {
    var id = e.detail.current;
    this.setData({ tabId: e.detail.current });
    if ((id == 0 && this.data.loaded == 0) || (id == 1 && this.data.loaded2 == 0)) {
      this.getList();
    }
    wx.setNavigationBarTitle({
      title: id == 0 ? '我赞过的' : '赞过我的'
    });
  },

  onLoad: function (options) {
    var that = this;
    var window = app.globalData.window;
    this.setData({ window: window })
    this.setData({ notiCount: options.notiCount });
    if (options.notiCount > 0) {
      this.setData({ tabId: 1 });
    }
    var user_info = wx.getStorageSync('user_info');
    var user_id = options.user_id ? options.user_id : user_info.user_id;
    that.setData({ type: options.type, user_id: user_id});
    this.getList();
    if (options.user_id){
      wx.setNavigationBarTitle({
        title: 'Ta 赞过的'
      });
    }
   
  },
  //删除提醒数
  delNoit(type) {
    var that = this;
    var notiCount = this.data.notiCount;
    if (notiCount > 0 && this.data.tabId == 1) {
      app.api("sns/notification/del", { type: type }, function (res) {
        console.log(res);
        that.setData({ notiCount: 0 });
      })
    }

  },
  //api evens/list 活动列表 
  getList() {
    var that = this;
    if (this.data.tabId==0 && ( this.data.loading == 1 || this.data.lastPage == 1)) {
       return;
    }
    if (this.data.tabId == 1 && (this.data.loading2 == 1 || this.data.lastPage2 == 1)) {
      return;
    }
    console.log(this.data.lastPage);
   
    if (this.data.tabId == 0){
      this.data.loading=1;
      var page = that.data.page;
    }else{
      this.data.loading2 = 1;
      var page = that.data.page2;
    }
    var param = {
      page: page,
      pagesize: 20,
    };

    if (this.data.tabId==0){
      param.user_id = this.data.user_id;
    }else{
      param.to_user_id = this.data.user_id;
    }
    
    //console.log(event_type);
    app.api("sns/topic/mythumbs", param, function (res) {
      
      if (that.data.tabId == 0) {
        that.data.loading = 0;
      } else {
        that.delNoit("thumbs");
        that.data.loading2 = 0;
      }
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      page++;
      if (res.data.list.length > 0) {
        if (that.data.tabId == 0) {
          that.setData({ listData: that.data.listData.concat(res.data.list) });
        }else{
          that.setData({ listData2: that.data.listData2.concat(res.data.list) });
        }
      } else {
        if (that.data.tabId == 0) {
          that.data.lastPage = 1;
        } else {
          that.data.lastPage2 = 1;
        }
        if (res.code != 200) {
          app.alert(res.msg);
          return;
        }
      }
      if (that.data.tabId == 0){
        that.data.page=page;
      }else{
        that.data.page2 = page;
      }
    })
  }
})
